package com.jinlei.crm.settings.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jinlei.crm.commons.exception.CustomException;
import com.jinlei.crm.settings.dao.DicTypeDao;
import com.jinlei.crm.settings.entity.DicType;
import com.jinlei.crm.settings.service.DicTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DicTypeServiceImpl implements DicTypeService {

    @Autowired
    private DicTypeDao dicTypeDao;

    @Override
    public DicType findByCode(String code) {
        return dicTypeDao.selectByCode(code);
    }

    @Override
    public Map<String, Object> listPage(Integer pageNumber, Integer pageSize, DicType dicType) {
        PageHelper.startPage(pageNumber, pageSize);
        PageInfo<DicType> pageInfo = new PageInfo<>(dicTypeDao.select(dicType));
        Map<String, Object> pageMap = new HashMap<>();
        pageMap.put("list", pageInfo.getList());
        pageMap.put("total", pageInfo.getTotal());
        return pageMap;
    }

    @Override
    public List<DicType> list() {
        return dicTypeDao.select(null);
    }

    @Override
    public boolean insert(DicType dicType) {
        // 判断用户名是否重复
        DicType dbDicType = dicTypeDao.selectByCode(dicType.getCode());
        if(dbDicType != null){
            throw new CustomException(-20,"当前数据字典类型编码已存在");
        }
        return dicTypeDao.insert(dicType) > 0;
    }

    @Override
    public boolean update(DicType dicType) {
        return dicTypeDao.update(dicType) > 0;
    }

    @Override
    public boolean remove(String code) {
        return dicTypeDao.deleteByCode(code) > 0;
    }

    @Override
    public boolean removeBatch(String[] codes) {
        return dicTypeDao.deleteBatch(codes) > 0;
    }
}
